﻿using System;
using fastCSharp;

namespace diantou.web.question
{
    /// <summary>
    /// 问题视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    class view<viewType> : web.view<viewType>
        where viewType : view<viewType>
    {
        /// <summary>
        /// 问题
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.question question;
        /// <summary>
        /// 用户问题关系
        /// </summary>
        protected userQuestion userQuestion
        {
            get { return new userQuestion { Question = question, UserId = currentUserId }; }
        }
        /// <summary>
        /// 关注者集合
        /// </summary>
        protected focusImage focusImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusQuestion.getUserIds(question.Id, diantou.dataModel.focus.type.Focus, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 相关收藏top5
        /// </summary>
        protected userQuestion.relateFavorite relateFavorite
        {
            get
            {
                return new userQuestion.relateFavorite { ViewOnly = requestHeader.IsReView, Question = new userQuestion { Question = question, UserId = currentUserId } };
            }
        }
        /// <summary>
        /// 当前用户邀请回答集合
        /// </summary>
        protected diantou.dataAccess.questionInvite[] inviteUsers
        {
            get
            {
                if (currentUserId == 0 || !isQuestionInvite) return null;
                return diantou.dataAccess.tcpCall.questionInvite.getQuestion(question.Id);
            }
        }
        /// <summary>
        /// 是否显示邀请回答
        /// </summary>
        protected virtual bool isQuestionInvite
        {
            get { return false; }
        }
        /// <summary>
        /// 是否显示答案连接
        /// </summary>
        protected virtual bool isAnswerPath
        {
            get { return true; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int questionId)
        {
            return load(questionId);
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(int questionId, string searchEnginePath = null)
        {
            if (questionId == 0)
            {
                checkSearchEngine(searchEnginePath, webConfig.NotFound404);
                return false;
            }
            question = diantou.dataAccess.clientCache.Question[questionId];
            if (question != null) return true;
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
